/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __1DA4F231_BCD6_4E5D_9D94_FC4ECDD6CB61__
#define __1DA4F231_BCD6_4E5D_9D94_FC4ECDD6CB61__

#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_box
{
    // 
    // notify_impl
    // 
    class notify_impl : public xos_stl::mem_item< xos::release_impl< notify_impl >, thread_lock >,
        public i_notify
    {
    public:
        typedef xos::release_impl< notify_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_xos_item_data( ITEM_DATA *& item_ptr );

    public:
        notify_impl();
        ~notify_impl();

    public:
        // 设置消息参数
        int set_threads( i_threads * pThreads, bool bUseSameThread );
        int set_thread( i_callback * pThread );
        int set_callback( xos::f_runnable pFun );
        int set_msg_type( int nType );

        // 通知方法
        int notify( i_msg *& pMsg, int nType, int nMsg );
        int notify( i_msg *& pMsg, int nMsg );
        int notify( i_msg *& pMsg );
        int notify_type( i_msg *& pMsg, int nType );
        int notify( int nMsg );

        // 得到当前线程，两种情况可以得到线程：
        // 1、set_threads以bUseSameThread为true调用
        // 2、set_thread设置过线程了
        i_callback * get_cur_thread();

        // 复制一个
        i_notify * clone();

    protected:
        xos::f_runnable m_pCallbackFun;
        int m_nMsgType;

        i_callback * m_pThread;
        i_threads * m_pThreads;
        i_engine * m_pEngine;

        bool m_bUseSameThread;

    private:
        int helper_notify( i_msg *& pMsg );
        int fill_msg( i_msg * pMsg ) const;
        int init_data();

    public:
        int set_engine( i_engine * pEngine );
        int init();
        int term();

    };
} // xos

#endif // __1DA4F231_BCD6_4E5D_9D94_FC4ECDD6CB61__
